var sliderWidth = 140; // 需要设置slider的宽度，用于计算中间位置
var app = getApp();
Page({
  data: {
    order_id: 0,
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    password: '',
    list: [],
    tabs: ['未领取', '已领取'],
    index:0,
    shops: []
  },
  onLoad: function (e) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });
    that.getOrders();
  },
  tabClick: function (e) {
    var state = e.currentTarget.id;
    // this.getOrders();
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
  },
  getOrders: function () {
    var that = this;
    app.http.get('bigwheel/my_award').then((data) => {
      var tabs = [
        '未领取(' + data.notuse + ')',
        '已领取(' + data.hasuse + ')'
      ];
      that.setData({
        list: data.list,
        tabs: tabs
      });
    });
  },
  powerDrawer: function (e) {
    var currentStatu = e.currentTarget.dataset.statu;
    this.setData({ order_id: e.currentTarget.dataset.id })
    this.util(currentStatu)
  },
  util: function (currentStatu) {
    /* 动画部分 */
    // 第1步：创建动画实例   
    var animation = wx.createAnimation({
      duration: 200,  //动画时长  
      timingFunction: "linear", //线性  
      delay: 0  //0则不延迟  
    });

    // 第2步：这个动画实例赋给当前的动画实例  
    this.animation = animation;

    // 第3步：执行第一组动画  
    animation.opacity(0).rotateX(-100).step();

    // 第4步：导出动画对象赋给数据对象储存  
    this.setData({
      animationData: animation.export()
    })

    // 第5步：设置定时器到指定时候后，执行第二组动画  
    setTimeout(function () {
      // 执行第二组动画  
      animation.opacity(1).rotateX(0).step();
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象  
      this.setData({
        animationData: animation
      })

      //关闭  
      if (currentStatu == "close") {
        this.setData(
          {
            showModalStatus: false
          }
        );
      }
    }.bind(this), 200)


    // 显示  
    if (currentStatu == "open") {
      this.setData(
        {
          showModalStatus: true
        }
      );
    }
  },

  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },

  use: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.logid;
    app.http.get('bigwheel/shops', {id:id}).then((data) => {
      that.setData({
        order_id: id,
        shops: data,
        showModalStatus: true
      });
    });
  },

  passInput: function (e) {
    var that = this;
    that.setData({
      password: e.detail.value
    });
  },

  doUse: function (e) {
    var that = this;
    var order_id = this.data.order_id;
    var shop = this.data.shops[this.data.index];
    var password = that.data.password;
    if (!password) {
      app.toast('请输入核销密码', 'error');
      return false;
    }
    if (!shop) {
      app.toast('请选择门店', 'error');
      return false;
    }
    app.http.post('bigwheel/do_use', {
      order_id: order_id, pwd: password, shop: shop
    }).then((data) => {
      that.setData({ order_id: 0 })
      that.getOrders()
      // 核销成功
      that.setData({
        showModalStatus: false,
        password: ''
      });
    }).catch((error) => {
      app.toast(error, 'error');
    });
  }

});
